<?php
/**
 * Created by PhpStorm.
 * User: 38339
 * Date: 2021-05-21
 * Time: 16:49
 */

namespace app\common\model\table;
use think\Model;

class CommonTableConfig extends Model
{
    protected $name = 'common_table_config';

    /**
     * @param $models
     * @return array
     */
    public static function fillModels($models)
    {
        $list = [];
        foreach ($models as $model) {
            $listItem = static::fillModel($model);
            $list[] = $listItem;
        }
        return $list;
    }

    public static function fillModel($model)
    {

        $data = [];
        $data['id'] = $model['id'];
        $data['table_name'] = $model['table_name'];
        $data['remark'] = $model['remark'];
        $data['create_time'] = $model['create_time'];
        $data['update_time'] = $model['update_time'];
        $data['status'] = (string)$model['status'];
        $data['is_generate'] = (string)$model['is_generate'];
        $data['table_namespace_id'] = $model['table_namespace_id'];
        $data['table_namespace_name'] = CommonTableNamespace::getOne(['id'=>$model['table_namespace_id']])['remark'];
        return $data;
    }

    /*
     * 获取列表
     */
    public static function getList($where,$limit_start,$page_size){
        $lists =  self::where($where)->order('id','desc')->limit($limit_start,$page_size)->select();
        return self::fillModels($lists);
    }

    public static function getListAll($where){
        return self::where($where)->order('id','desc')->select();
    }

    /*
     * 获取总数据条数
     */
    public static function getCount($where){
        return self::where($where)->count();
    }

    /*
     * 获取单条数据
     */
    public static function getOne($where,$isHandle = false){
        if($isHandle){
            return self::fillModel(self::where($where)->find());
        }else{
            return self::where($where)->find();
        }
    }

}